//
//  YZMWithdrawPwdViewController.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/18.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMWithdrawPwdViewController.h"
#import "YZMHintInputView.h"
#import "YZClient+Account.h"
#import "YZGeneralResultModel.h"

@interface YZMWithdrawPwdViewController () <UITextFieldDelegate>

@property (nonatomic, strong) YZMHintInputView *passwordView;
@property (nonatomic, strong) YZMHintInputView *ConfirmPwdView;
@property (nonatomic, strong) NSDictionary *params;

@end

@implementation YZMWithdrawPwdViewController {
    
    UIButton *_setwithDrawButton;
    BOOL _pwdValidateFlag;
    BOOL _ConfirmPwdValidateFlag;
}

- (id)init {
    if (self = [super init]) {
        self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
        self.hidesBottomBarWhenPushed = YES;
        self.title = @"设置提现密码";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createObjects];
}

- (void)createObjects {
    
    WS(ws)
    
    self.passwordView = [[YZMHintInputView alloc] init];
    [self.view addSubview:self.passwordView];
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.view).with.offset(14);
        make.left.equalTo(ws.view);
        make.width.equalTo(ws.view);
        make.height.mas_equalTo(InputViewHeight);
    }];
    
    self.passwordView.inputLabel.text = @"提现密码";
    self.passwordView.textField.placeholder = @"请输入6位数字";
    self.passwordView.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.passwordView.textField.returnKeyType = UIReturnKeyNext;
    self.passwordView.textField.secureTextEntry = YES;
    self.passwordView.textField.delegate = self;
    
    self.passwordView.lineTopView.hidden=NO;
    
    [self.passwordView.textField addTarget:self action:@selector(textFieldchange:) forControlEvents:UIControlEventEditingChanged];
    
    self.ConfirmPwdView = [[YZMHintInputView alloc] init];
    [self.view addSubview:self.ConfirmPwdView];
    [self.ConfirmPwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.passwordView.mas_bottom);
        make.left.width.height.equalTo(ws.passwordView);
    }];
    
    self.ConfirmPwdView.inputLabel.text = @"确认密码";
    self.ConfirmPwdView.textField.placeholder = @"请再次提现密码";
    self.ConfirmPwdView.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.ConfirmPwdView.textField.returnKeyType = UIReturnKeyNext;
    self.ConfirmPwdView.textField.secureTextEntry = YES;
    
    self.ConfirmPwdView.textField.returnKeyType = UIReturnKeyDone;
    self.ConfirmPwdView.textField.delegate = self;
    [self.ConfirmPwdView.textField addTarget:self action:@selector(textFieldchange:) forControlEvents:UIControlEventEditingChanged];
    
    self.ConfirmPwdView.lineBottomView.hidden=NO;
    
    self.passwordView.lineBottomView.hidden = YES;
    [self.ConfirmPwdView updateLineIndentationMethod:self.ConfirmPwdView.lineTopView enumeration:LINE_INDENT_SCENE_LEFT indentValue:ControlSpacing15];

    _setwithDrawButton = [YZMAppStyle NewsubmitButton];
    [_setwithDrawButton setTitle:@"确定" forState:UIControlStateNormal];
    [_setwithDrawButton addTarget:self action:@selector(confirmButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    _setwithDrawButton.enabled = NO;
    [self.view addSubview:_setwithDrawButton];
    [_setwithDrawButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view).with.offset(ControlSpacing20);
        make.right.equalTo(ws.view).with.offset(-ControlSpacing20);
        make.top.equalTo(ws.ConfirmPwdView.mas_bottom).with.offset(ControlSpacing30);
    }];

}

+ (instancetype)createWithParams: (NSDictionary *)params
{
    YZMWithdrawPwdViewController *ctrl = [[self alloc] init];
    [ctrl updateWithParams: params];
    return ctrl;
}

- (void)updateWithParams:(NSDictionary *)params{
    self.params = params;
}

#pragma mark -- set withdraw password

- (void)confirmButtonPressed {
    [self resignAllFirstResponder];
    
    if (![self checkInputContent:self.passwordView.textField] ||
        ![self checkInputContent:self.ConfirmPwdView.textField]) {
        return;
    }
    
    if (![self.passwordView.textField.text isEqualToString:self.ConfirmPwdView.textField.text]) {
        [MBProgressHUD showMessage: @"两次密码输入不一致"];
        return;
    }
    
    [YZClient setDrawPwdWithPwd: self.passwordView.textField.text  success:^(NSURLSessionDataTask *task,id responseObject) {
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            YZMUserServiceInstance.user.detail.isWithDrawPw = YES;
            [MBProgressHUD showMessage: @"提现密码设置成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.params) {
                    if ([self.params.allKeys containsObject:@"pageName"]) {
                        NSString *vcName = self.params[@"pageName"];
                        if ([vcName isEqualToString:@"draw"]) {
                            [self.navigationController popToRootViewControllerAnimated:NO];
                            [[NSNotificationCenter defaultCenter] postNotificationName:@"pushdraw" object:nil];
                            return;
                        }else{
                            if ([self.params.allKeys containsObject:@"vcName"]) {
                                [YZMHCMInstance popViewController:self.params[@"vcName"] animated:YES];
                                 return;
                            }
                           
                        }
                        
                    }
                }
               
                [YZMHCMInstance popViewController:nil animated:YES];
            });
        } else
        {
            [topModel showMessage];
        }
    } failure:^(NSError *error) {
        
        [error yz_showMessage];
    }];
    
}

- (BOOL)checkInputContent:(UITextField *)textField {
    
    if (textField == self.passwordView.textField || textField == self.ConfirmPwdView.textField) {
        
        if ([self.passwordView.textField.text yz_isWithdrawPwdCode]) {
            return YES;
        } else {
            [self registerValidateFail:@"提现密码由6位纯数字组成"];
            return NO;
        }
    }
    
    return NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)resignAllFirstResponder {
    [self.passwordView.textField resignFirstResponder];
    [self.ConfirmPwdView.textField resignFirstResponder];
}

#pragma mark -- UITextFieldDelegate

//点击键盘return
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField == self.passwordView.textField) {
        
        [self.ConfirmPwdView.textField becomeFirstResponder];
    } else if (textField == self.ConfirmPwdView.textField) {
        
        [self.ConfirmPwdView.textField resignFirstResponder];
    }
    
    return YES;
}

- (void)textFieldchange:(UITextField *)send {
    
    if ([self.passwordView.textField.text length] > 0 &&
        [self.ConfirmPwdView.textField.text length] > 0) {
        _setwithDrawButton.enabled = YES;
        _setwithDrawButton.layer.borderWidth=0.0;
        return;
    }
    
    _setwithDrawButton.layer.borderWidth=1.0;
    _setwithDrawButton.enabled = NO;
}

#pragma mark -- registerValid

- (void)registerValidateFail:(NSString *)message {

    if (message) {
        [MBProgressHUD showMessage:message];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
